<?php
/**
 * 街道
 * 
 * Created	: 2010-11-23
 * Modified : 2010-11-23
 * @author  : 陈立强
 */
class Street{

	public static $cfg = null;
	public static $cfgName = 'build/street';

	/**
	 * 初始化生成街道数据
	 * @return boolean
	 */
	public static function init($init_num=10)
	{
		global $DB, $L;
		foreach (self::$cfg as $area=>$cfg) {
			self::create($area, $init_num);
			$area++;
		}
		return true;
	}

	/**
	 * 在指定区域内创建街道
	 * @param int $area
	 * @param int $num
	 */
	public static function create($area, $num=5)
	{
		global $DB, $L;

		$sql = 'SELECT max(orderid) FROM street WHERE area=:area';
		$param = array('area'=>$area);
		$orderid = $DB->getValue($sql, $param);
		$orderid++;

		$streetIdList = array();
		for ($i=0; $i<$num; $i++) {
			$data = array(
				'uid'     => 0,
				'area'    => $area,
				'orderid' => $orderid,
			);
			$streetId = $DB->insert('street', $data);
			$streetIdList[] = $streetId;
			$orderid++;
			Uptown::init($streetId);
		}
		
		return $streetIdList;
	}

	/**
	 * 街道内拥有的店铺数量
	 * @param int $street_id
	 * @return int
	 */
	public static function haveShopNumInStreet($street_id)
	{
		global $DB, $L, $G;
		$street_id = (int)$street_id;
		$sql = 'SELECT count(*) FROM user_shop WHERE street_id='.$street_id;
		$cnt = $DB->getValue($sql);

		return $cnt;
	}

	/**
	 * 区域内是否有足够的店铺位置
	 * @param int $area
	 * @return array
	 */
	public static function haveEnoughShopSpaceInArea($area)
	{
		global $DB, $L, $G;

		// 获取区域内总店铺数
		$param = array('area'=>$area);
		$sql = "SELECT count(*) FROM user_shop WHERE area=:area";
		$shopCnt = $DB->getValue($sql, $param);
		// 获取区域内街道数
		$sql = "SELECT count(*) FROM street WHERE area=:area";
		$streetCnt = $DB->getValue($sql, $param);
		
		// 店铺空位小于50个，区域内要新增5条街道
		if (($streetCnt*$G['shop_num_per_street'] - $shopCnt) <= $G['min_remain_space']) {
			return false;
		} else {
			return true;
		}
	}

}